// pages/center/index.js
const {request ,loginApi} = require('../../utils/api.js');
var app = new getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    showArea: false,
    navH: 0,
    isIphoneX: false,
    userInfo: null,
    content:null,
    user: {},
    name:'',
    phone:'',
    addr: '',
    message:'',
    area:'',
    p_id:'',
    c_id:'',
    a_id:'',
    shenfen: 0
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (!getApp().checkLogin()) return
    //data 
    let isIphoneX = app.globalData.isIphoneX
    if (isIphoneX) {
      this.setData({
        isIphoneX
      })
    }
    this.setData({
      navH: app.globalData.navHeight
    })
  },
  submit() {
    if (!getApp().checkLogin()) return
    const submitData = {
      member_id: this.data.user.member_id,
      name:  this.data.name,
      phone:  this.data.phone,
      area: this.data.area,
      addr:  this.data.addr,
      message:  this.data.message,
      p_id: this.data.p_id,
      c_id: this.data.c_id,
      a_id: this.data.a_id,
    }
    console.log(submitData)
    if(!submitData.name){
      wx.showToast({
        title: '请输入姓名',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if(!submitData.phone){
      wx.showToast({
        title: '请输入手机号码',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if(!submitData.p_id || !submitData.c_id || !submitData.a_id ){
      wx.showToast({
        title: '请输入省市区',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if(!submitData.addr){
      wx.showToast({
        title: '请输入详细地址',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if(!submitData.message){
      wx.showToast({
        title: '请输入您的需求',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    request('/sub_message', 'POST', submitData).then(res => {
      console.log(res)
      if (res.data.code != 200) {
        wx.showToast({
          title: res.data.msg,
          icon: 'error',
          duration: 2000
        })
        return;
      }
      wx.showToast({
        title: '提交成功',
        icon: 'success',
        duration: 2000
      })
      this.setData({
        name:  '',
        phone: '',
        area: '',
        addr:  '',
        message:  '',
        p_id: '',
        c_id: '',
        a_id: '',
      })
    })
  },
  toPage(e) {
    if (!getApp().checkLogin()) return
    if (e.currentTarget.dataset.url == '/pages/apply/apply') {
      request('/search_agent', 'POST', {member_id: this.data.user.member_id}).then(res => {
        if (res.data.code == 201) {
          wx.showToast({
            title: '请等待审核通过',
            icon: 'error',
            duration: 2000
          })
          return;
        } else if (res.data.code == 404) {
          wx.navigateTo({
            url: e.currentTarget.dataset.url,
          })
        }
      })
    } else {
      wx.navigateTo({
        url: e.currentTarget.dataset.url,
      })
    }
  },
  toPersonal() {
    if (!getApp().checkLogin()) return
    wx.navigateTo({
      url: '/pages/personal-info/personal-info',
    })
  },
  toAppointment(){
    if (!getApp().checkLogin()) return
    wx.navigateTo({
      url: '/pages/appointment/appointment',
    })
  },
  takePhone(){
    if(this.data.userInfo.member.phone){
      return
    }
    let _that = this;
    wx.navigateTo({
      url: '/pages/grant/grant',
    })
    return;
    wx.makePhoneCall({
      phoneNumber: _that.data.userInfo.member,
    })
  },
  
  showPoup(e) {
    this.setData({
      show: true,
      pickerTitle: '选择'+e.currentTarget.dataset.title
    })
  },
  
  close(e){
    console.log(e)
    this.setData({
      showArea:false,
      area:e.detail.value&&e.detail.value.join(","),
      p_id:e.detail.pid,
      c_id:e.detail.cid,
      a_id:e.detail.aid
    })
  },
  showArea() {
    this.setData({
      showArea: true
    })
    console.log(111)
  },
  onClose() {
    this.setData({ show: false });
  },
  onChange(e) {
    console.log(e.currentTarget.dataset.name)
    const param = e.currentTarget.dataset.name; //字段名 
    const value = e.detail; 
    // this.setData({
    //   [param]: value
    // })
  },
  onConfirm() {},
  onCancel() {},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let userInfo = wx.getStorageSync('user')
    if (userInfo && userInfo.id) {
      this.setData({
        user: userInfo,
        shenfen: userInfo.member.shenfen
      })
    }
    //判断是否登录
    let user = loginApi.cache("user")
    if (user.nickname && user.headimgurl) {
      this.setData({
        userInfo: user
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      // imageUrl: '../../imgs/index/share.jpg',
      // title: app.globalData.shareTitle,
      // path: '/pages/index/index'
    }
  }
})